<?php
/**
 * Created by PhpStorm.
 * User: zy-l
 * Date: 2019/6/19
 * Time: 1:23 AM
 */

namespace App\HttpController\Admin;

use App\HttpController\AdminBase;
use EasySwoole\Validate\Validate;

/**
 * 联系管理
 * Class Adv
 * @package App\HttpController\Admin
 */
class Infocase extends AdminBase
{
    /**
     * 案例模型
     * @return \App\Model\InfoCase
     */
    public function getInfoCaseModel()
    {
        return new \App\Model\InfoCase();
    }

    /**
     * 案例列表
     * @return string|void
     */
    public function index()
    {
        if ($this->isPost()) {
            $where = [];
            return $this->writePageJson('获取列表数据', $this->getInfoCaseModel()->getPageAll($where, $this->postParam('page')));
        }
        return $this->fetch('index');
    }

    /**
     *  添加
     * @return bool|string|void
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function add()
    {
        if ($this->isPost()) {
            if ($this->isPost()) {
                $data = $this->postParams();
                return $this->writeJson('操作成功', $this->getInfoCaseModel()->updateOrCreateData($data, ["id" => $this->postParam("id")]));
            }
        }
        return $this->fetch('add');
    }

    /**
     * 编辑
     * @return bool|string|void
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function edit()
    {
        $id = $this->getParam('id');
        if ($this->isPost()) {
            if ($this->isPost()) {
                $data = $this->postParams();
                return $this->writeJson('操作成功', $this->getInfoCaseModel()->updateOrCreateData($data, ["id" => $this->postParam("id")]));
            }
        }
        $info = $this->getInfoCaseModel()->findOne(["id" => $id]);
        $this->assign(["data" => json_encode($info)]);
        return $this->fetch('edit');
    }

    /**
     * 删除
     * @return bool
     */
    public function del()
    {
        return $this->writeJson('删除成功', $this->getInfoCaseModel()->delData(['id' => $this->postParam('id')]));
    }

    /**
     * 提交参数验证
     * @param string|null $action
     * @return Validate|null
     */
    protected function getValidateRule(?string $action): ?Validate
    {
        $validate = null;
        return $validate;
    }

}